package cn.iocoder.yudao.framework.common.util.sign;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * 加密方式：AES- 256
 * 加密模式：ECB
 * 秘钥长度：32bytes
 */
public class AESUtil {
    private static final String ALGORITHM = "AES";
    private static final String MODE = "AES/ECB/PKCS5Padding";

    public static String encrypt(String plaintext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(MODE);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String ciphertext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(MODE);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
        return new String(decryptedBytes);
    }

    public static void main(String[] args) throws Exception {
        //入参格式要和一致，换行、空格、顺序不一致、字符串不一样，会导致加密结果不一样。
        String plaintext = "[{\"cardNumber\":\"123456\",\"expiryDate\":\"2016-10-10\",\"password\":\"654321\"},{\"cardNumber\":\"654321\",\"expiryDate\":\"2016-10-10\",\"password\":\"123456\"}]";
        String key = "12345678901234567890123456789012";
        String ciphertext = encrypt(plaintext, key);
        System.out.println("Ciphertext: " + ciphertext);
        String decryptedText = decrypt(ciphertext, key);
        System.out.println("Decrypted text: " + decryptedText);
    }
}
/*
    QZltw6QjIQ55tmU0xg2+20FGGnnmGoTWN/KZUdsllLoyaFGsi/dx7lc/JSPb/rTDUmcbeGakkBwlMdwkG51DABZQnmd6VC0x/tQi2RQ/BaE20LGmfpL0GkeXxUcTFRaf99lSLAqi1rkS6N4WxwL0jyCOi114VSzT06SZpOe2HqpLVPa6HuW0IOa9xFKdz9AZ
    QZltw6QjIQ55tmU0xg2+20FGGnnmGoTWN/KZUdsllLoyaFGsi/dx7lc/JSPb/rTDUmcbeGakkBwlMdwkG51DABZQnmd6VC0x/tQi2RQ/BaE20LGmfpL0GkeXxUcTFRaf99lSLAqi1rkS6N4WxwL0jyCOi114VSzT06SZpOe2HqpLVPa6HuW0IOa9xFKdz9AZ
    QZltw6QjIQ55tmU0xg2+20FGGnnmGoTWN/KZUdsllLoyaFGsi/dx7lc/JSPb/rTDUmcbeGakkBwlMdwkG51DABZQnmd6VC0x/tQi2RQ/BaE20LGmfpL0GkeXxUcTFRaf99lSLAqi1rkS6N4WxwL0jyCOi114VSzT06SZpOe2HqpLVPa6HuW0IOa9xFKdz9AZ
agentOrderNo123456789cardInfoQZltw6QjIQ55tmU0xg2+20FGGnnmGoTWN/KZUdsllLoyaFGsi/dx7lc/JSPb/rTDUmcbeGakkBwlMdwkG51DABZQnmd6VC0x/tQi2RQ/BaE20LGmfpL0GkeXxUcTFRaf99lSLAqi1rkS6N4WxwL0jyCOi114VSzT06SZpOe2HqpLVPa6HuW0IOa9xFKdz9AZisSuccessTjdOrderNo123456789status1time20140819122322timestamp2012320110243200version1.0
agentOrderNo123456789cardInfoQZltw6QjIQ55tmU0xg2+20FGGnnmGoTWN/KZUdsllLoyaFGsi/dx7lc/JSPb/rTDUmcbeGakkBwlMdwkG51DABZQnmd6VC0x/tQi2RQ/BaE20LGmfpL0GkeXxUcTFRaf99lSLAqi1rkS6N4WxwL0jyCOi114VSzT06SZpOe2HqpLVPa6HuW0IOa9xFKdz9AZisSuccessTjdOrderNo123456789status1time20140819122322timestamp2012320110243200version1.0null
*/

